using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.Controls;
using WP.Modules;
using WP.DB;

namespace WP.Modules
{
    /// <summary>
    /// 
    /// </summary>
    public partial class frmFax : BaseQryRpt
    {
        public string FaxNo
        {
            set
            {
                txtFaxNo.Text = value;
            }
        }
        public string To
        {
            set
            {
                txtTo.Text = value;
            }
        }
        #region Define Variable
        private string mstrFaxTitle = string.Empty;
        private int mIntJobType;
        
        private string mstrCustSupNo = string.Empty;
        private string mstrLocNo = string.Empty;
        private bool mblnOK;
        private string mstrToName = string.Empty;
        private string mstrReturn = string.Empty;
       
        private FaxEmailType mModule = FaxEmailType.Other; 
        private DBAccess mdbcon;
        #endregion

        #region Constructor Functions

        public frmFax()
        {
            InitializeComponent();
            this.MinimumSize = this.Size; 
            mdbcon = new DBAccess(false, false);
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strTitle"></param>
        /// <param name="intJobType"></param>
        /// <param name="strModule">default""</param>
        /// <param name="strCondition">default ""</param>
        /// <param name="strSubject"></param>
        /// <param name="intType">default 1</param>
        /// <param name="blnIsForMultBOl">default false</param>
        /// <param name="strToName">default ""</param>
        /// <returns></returns>
        public bool GetFaxTitle(ref string strTitle, FaxJobType intJobType, FaxEmailType moduleType, string strCondition, string strSubject, int intType, bool blnIsForMultBOl,  string strToName,bool blnDefault)
        {
            string strSql  = string.Empty;
            string strSendTo  = string.Empty;
            string strSendToName  = string.Empty;
            string strSupNo  = string.Empty;
            string strTmpSendTo  = string.Empty;
            string strSysName  = string.Empty;
            bool blnMustUseBillToForInvWhenFaxAndEmail = false;
            mblnOK = false;
    
            if ((SysVar.CodeSignNo == SysVar.DefineCompany.Eacan || SysVar.CodeSignNo == SysVar.DefineCompany.EacanCanada) &&
                moduleType == FaxEmailType.Transportation1  )
            {
                strSupNo = strToName;
                strToName = string.Empty;
            }
            mIntJobType = (int)intJobType;
            mModule = moduleType;

            
            DataFunctions.DelTemporyTable(mdbcon,"#tmpLog");
            strSql = "CREATE TABLE #tmpLog (" +
                     " [Recid] int NOT NULL IDENTITY (1, 1),"+
                     " [Sys] [Char] (3) NULL,"+
                     " [Invno] [Char] (8) NULL,"+
                     " [CustNo] [Char] (8) NULL "+
                     " ) ON [PRIMARY]";
            mdbcon.ExecuteSql(strSql);

            strSendTo = string.Empty;
            strSendToName = string.Empty;
            DataView dvModule = new DataView();
            DataView dvInfo = new DataView();
            DataView dvShipToInfo = new DataView();
            DataView dvCust = new DataView();
            switch (moduleType)
            {
                case FaxEmailType.Customer:
                    #region AR
                    strSql = "select recid,Fax, Email,Contact,CustNo from arCustomer " + (strCondition.Length > 0 ? " where " + strCondition : " ") +
                        " order by CustNo";
                    dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    foreach (DataRowView drModule in dvModule)
                    {
                        mstrCustSupNo = drModule["CustNo"].ToString();
                        switch (intJobType)
                        {
                            case FaxJobType.intFax:
                                strSendTo += drModule["Fax"].ToString().Trim() + ";";
                                break;
                            case FaxJobType.intEmail:
                                strSendTo += drModule["Email"].ToString().Trim() + ";";
                                break;
                        }
                        strSendToName += drModule["Contact"].ToString().Trim() + ";";
                    }
                    break;
                    #endregion
                case FaxEmailType.JobControl:
                    #region JOB
                    this.ubtfaxNo.Visible = true;
                    strSysName = "JOB";

                    strSql = "select  JobCustNo as CustNo, Jobno from oeBidJobControlHdr "+
                             " where Jobno in ( select min(Jobno) from oeBidJobControlHdr " +
                             (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " group by JobCustNo ) order by JobNo";
                    dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    strSql = "select recid,CustNo,Fax,Email,Contact,BillToFax  from arCustomer where custno in (select   JobCustNo from oeBidJobControlHdr "+
                        (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " )";
                    dvInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;

                    foreach (DataRowView drModule in dvModule)
                    {
                        dvInfo.RowFilter = "CustNo='" + drModule["CustNo"].ToString() + "'";
                        mstrCustSupNo = drModule["CustNo"].ToString();
                        if (dvInfo.Count >0)
                        {
                            switch(intJobType)
                            {
                                case FaxJobType.intFax:
                                    if (intType >= 3 && SysVar.CodeSignNo == SysVar.DefineCompany.Pacific 
                                        && dvInfo[0]["BillToFax"].ToString().Trim() != string.Empty)
                                        strSendTo += dvInfo[0]["BillToFax"].ToString().Trim() + ";";
                                    else
                                        strSendTo += dvInfo[0]["Fax"].ToString().Trim() + ";";
                                    break;
                                case FaxJobType.intEmail:
                                    strSendTo += dvInfo[0]["Email"].ToString().Trim() + ";";
                                    break;
                            }
                            strSendToName += dvInfo[0]["Contact"].ToString().Trim() + ";";
                        }
                        strSql = "INSERT INTO #tmpLog ( Sys, Invno ,CustNo) " +
                                 " VALUES ('" + strSysName + "','" + drModule["Jobno"].ToString() + "','" + drModule["CustNo"].ToString().Trim() + "')";
                        mdbcon.ExecuteSql(strSql);
                    }
                    break;
                    #endregion 
                case FaxEmailType.Bid  :
                    #region BID
                    break;
                    #endregion
                case FaxEmailType.Quotation:
                #region Quotation
                    this.ubtfaxNo.Visible = true;
                    strSysName = "OE";                   
                    {
                        strSql = "select  CustNo, QuoteNo from oeQuoteHdr " +
                                 " where QuoteNo in ( select min(QuoteNo) from oeQuoteHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " group by CustNo ) order by QuoteNo";
                        dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                        strSql = "select  recid,CustNo,Fax,Email,Contact,BillToFax  from arCustomer where custno in (select   CustNo from oeQuoteHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " )";
                        dvInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                        strSql = "select  a.recid,a.CustNo,a.Fax,a.Emailaddress,a.Contact,b.QuoteNo  from arshipto a inner join (select   CustNo,ShipToNo,QuoteNo from oeQuoteHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " ) b on a.custno=b.custno and a.shiptono=b.shiptono ";
                        dvShipToInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    }
                    bool blnQuoteUseShipTo = Customize.UseShipToWhenFax();
                    foreach (DataRowView drModule in dvModule)
                    {
                        dvInfo.RowFilter = "CustNo='" + drModule["CustNo"].ToString() + "'";
                        dvShipToInfo.RowFilter = "Quoteno='" + drModule["Quoteno"].ToString() + "'";
                        mstrCustSupNo = drModule["CustNo"].ToString();
                        bool blnShipToInfo = false;
                        bool blnShipToContactInfo = false;

                        if (blnQuoteUseShipTo && dvShipToInfo.Count > 0)
                        {
                            switch (intJobType)
                            {
                                case FaxJobType.intFax:
                                    if (dvShipToInfo[0]["Fax"].ToString().Trim().Length > 0)
                                    {
                                        strSendTo += dvShipToInfo[0]["Fax"].ToString().Trim() + ";";

                                        blnShipToInfo = true;
                                    }
                                    break;
                                case FaxJobType.intEmail:
                                    if (dvShipToInfo[0]["Emailaddress"].ToString().Trim().Length > 0)
                                    {
                                        strSendTo += dvShipToInfo[0]["Emailaddress"].ToString().Trim() + ";";

                                        blnShipToInfo = true;
                                    }
                                    break;
                            }
                            if (dvShipToInfo[0]["Contact"].ToString().Trim().Length > 0)
                            {
                                strSendToName += dvShipToInfo[0]["Contact"].ToString().Trim() + ";";
                                blnShipToContactInfo = true;
                            }

                        }

                        if (!blnShipToInfo && dvInfo.Count > 0)
                        {
                            switch (intJobType)
                            {
                                case FaxJobType.intFax:
                                    if (intType >= 3 && SysVar.CodeSignNo == SysVar.DefineCompany.Pacific
                                        && dvInfo[0]["BillToFax"].ToString().Trim() != string.Empty)
                                        strSendTo += dvInfo[0]["BillToFax"].ToString().Trim() + ";";
                                    else
                                        strSendTo += dvInfo[0]["Fax"].ToString().Trim() + ";";
                                    break;
                                case FaxJobType.intEmail:
                                    strSendTo += dvInfo[0]["Email"].ToString().Trim() + ";";
                                    break;
                            }

                        }
                        if (!blnShipToContactInfo && dvInfo.Count > 0)
                        {
                            strSendToName += dvInfo[0]["Contact"].ToString().Trim() + ";";
                        }
                        strSql = "INSERT INTO #tmpLog ( Sys, Invno ,CustNo) " +
                                 " VALUES ('" + strSysName + "','" + drModule["QuoteNo"].ToString() + "','" + drModule["CustNo"].ToString().Trim() + "')";
                        mdbcon.ExecuteSql(strSql);
                    }
                    if (intJobType == FaxJobType.intEmail)
                    {
                        strTmpSendTo = strSendTo;
                        strSql = "select recid,CustNo,ShipToNo,Fax,Contact,Emailaddress from arShipTo where custno+shiptono in (select   CustNo+shiptono from oeQuoteHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " ) order by custno,shiptono";
                        DataView dvTable = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                        if (dvTable.Count > 0)
                            strSendTo = dvTable[0]["Emailaddress"].ToString().Trim() + ";";
                        if (strSendTo.Trim() == ";")
                            strSendTo = strTmpSendTo;
                    }
                    break;
                #endregion
                case FaxEmailType.SaledOrder  :
                    #region O/E
                    this.ubtfaxNo.Visible = true;
                    strSysName = "OE";
                    if (blnIsForMultBOl)
                    {
                        strSql = "select  CustNo, invno,Ordstatus  from oeInvoiceHdr " +
                                 " where invno in ( select min(invNo) from oeInvoiceHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " group by CustNo ) order by InvNo";
                        dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                        strSql = "select  recid,CustNo,Fax,Email,Contact,BillToFax  from arCustomer where custno in (select   CustNo from oeInvoiceHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " )";
                        dvInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    }
                    else
                    {
                        strSql = "select  CustNo, invno,  Ordstatus from oeOrderHdr " +
                                 " where invno in ( select min(invNo) from oeOrderHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " group by CustNo ) order by InvNo";
                        dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                        strSql = "select  recid,CustNo,Fax,Email,Contact,BillToFax  from arCustomer where custno in (select   CustNo from oeOrderHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " )";
                        dvInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                        strSql = "select  a.recid,a.CustNo,a.Fax,a.Emailaddress,a.Contact,b.InvNo,a.billtofax,a.BillEmail,a.BillToContact  from arshipto a inner join (select   CustNo,ShipToNo,InvNo from oeOrderHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " ) b on a.custno=b.custno and a.shiptono=b.shiptono ";
                        dvShipToInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    }
                    bool blnUseShipTo = Customize.UseShipToWhenFax();
                    blnMustUseBillToForInvWhenFaxAndEmail = Customize.MustUseBillToForInvWhenFaxAndEmail(); 
                    foreach(DataRowView drModule in dvModule)
                    {
                        dvInfo.RowFilter = "CustNo='" + drModule["CustNo"].ToString() + "'";
                        dvShipToInfo.RowFilter = "invno='" + drModule["invno"].ToString() + "'";
                        mstrCustSupNo = drModule["CustNo"].ToString();
                        bool blnShipToInfo = false;
                        bool blnShipToContactInfo = false;
                        bool blnIsInvoice = false;
                        blnIsInvoice = CvtFunc.ToBoolean( CvtFunc.ToInt ( drModule["OrdStatus"])>=50 ? true : false );
                        if (blnMustUseBillToForInvWhenFaxAndEmail && blnIsInvoice && !blnIsForMultBOl && dvShipToInfo.Count > 0)
                        {
                            switch (intJobType)
                            {
                                case FaxJobType.intFax:
                                    if (dvShipToInfo[0]["billtofax"].ToString().Trim().Length > 0)
                                    {
                                        strSendTo += dvShipToInfo[0]["billtofax"].ToString().Trim() + ";";
                                        blnShipToInfo = true;
                                    }
                                    else
                                    {
                                        strSendTo += dvInfo[0]["Fax"].ToString().Trim() + ";";
                                        blnShipToInfo = true;
                                    }
                                    break;
                                case FaxJobType.intEmail:
                                    if (dvShipToInfo[0]["BillEmail"].ToString().Trim().Length > 0)
                                    {
                                        strSendTo += dvShipToInfo[0]["BillEmail"].ToString().Trim() + ";";
                                        blnShipToInfo = true;
                                    }
                                    else
                                    {
                                        strSendTo += dvInfo[0]["Email"].ToString().Trim() + ";";
                                        blnShipToInfo = true;
                                    }
                                    break;
                            }
                            if (dvShipToInfo[0]["BillToContact"].ToString().Trim().Length > 0)
                            {
                                strSendToName += dvShipToInfo[0]["BillToContact"].ToString().Trim() + ";";
                                blnShipToContactInfo = true;
                            }                            
                        }
                        else if (blnMustUseBillToForInvWhenFaxAndEmail && blnIsInvoice && !blnIsForMultBOl && dvShipToInfo.Count == 0)
                        {
                            switch (intJobType)
                            {
                                case FaxJobType.intFax:                                   
                                     strSendTo += dvInfo[0]["Fax"].ToString().Trim() + ";";
                                     blnShipToInfo = true;                                    
                                    break;
                                case FaxJobType.intEmail:
                                    strSendTo += dvInfo[0]["Email"].ToString().Trim() + ";";
                                    blnShipToInfo = true;                                    
                                    break;
                            }
                            strSendToName += dvInfo[0]["Contact"].ToString().Trim() + ";";
                            blnShipToContactInfo = true;
                            
                        }
                        else if (blnUseShipTo  && dvShipToInfo.Count > 0)
                        {
                            switch (intJobType)
                            {
                                case FaxJobType.intFax:
                                    if (dvShipToInfo[0]["Fax"].ToString().Trim().Length > 0)
                                    {
                                        strSendTo += dvShipToInfo[0]["Fax"].ToString().Trim() + ";";
                                        
                                        blnShipToInfo = true;
                                    }
                                    break;
                                case FaxJobType.intEmail:
                                    if (dvShipToInfo[0]["Emailaddress"].ToString().Trim().Length > 0)
                                    {
                                        strSendTo += dvShipToInfo[0]["Emailaddress"].ToString().Trim() + ";";
                                        
                                        blnShipToInfo = true;
                                    }
                                    break;
                            }
                            if (dvShipToInfo[0]["Contact"].ToString().Trim().Length > 0)
                            {
                                strSendToName += dvShipToInfo[0]["Contact"].ToString().Trim() + ";";
                                blnShipToContactInfo = true;
                            }
                            
                        }
                        if (!blnMustUseBillToForInvWhenFaxAndEmail)
                        {
                            if (!blnShipToInfo && dvInfo.Count > 0)
                            {
                                switch (intJobType)
                                {
                                    case FaxJobType.intFax:
                                        if (intType >= 3 && SysVar.CodeSignNo == SysVar.DefineCompany.Pacific
                                            && dvInfo[0]["BillToFax"].ToString().Trim() != string.Empty)
                                            strSendTo += dvInfo[0]["BillToFax"].ToString().Trim() + ";";
                                        else
                                            strSendTo += dvInfo[0]["Fax"].ToString().Trim() + ";";
                                        break;
                                    case FaxJobType.intEmail:
                                        strSendTo += dvInfo[0]["Email"].ToString().Trim() + ";";
                                        break;
                                }

                            }
                        }
                    
                        if (!blnShipToContactInfo && dvInfo.Count > 0)
                        {
                            strSendToName += dvInfo[0]["Contact"].ToString().Trim() + ";";
                        }
                        strSql = "INSERT INTO #tmpLog ( Sys, Invno ,CustNo) " +
                                 " VALUES ('" + strSysName + "','" + drModule["InvNo"].ToString() + "','" + drModule["CustNo"].ToString().Trim() + "')";
                        mdbcon.ExecuteSql(strSql);
                    }
                    if (intJobType == FaxJobType.intEmail && !blnMustUseBillToForInvWhenFaxAndEmail)
                    {
                        strTmpSendTo = strSendTo;
                        strSql = "select recid,CustNo,ShipToNo,Fax,Contact,Emailaddress from arShipTo where custno+shiptono in (select   CustNo+shiptono from oeorderHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " ) order by custno,shiptono";
                        DataView dvTable = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                        if (dvTable.Count>0)
                            strSendTo = dvTable[0]["Emailaddress"].ToString().Trim() + ";";
                        if (strSendTo.Trim() == ";")
                            strSendTo = strTmpSendTo;
                    }
                break;
                #endregion
                case FaxEmailType.SalesReturn:
                    #region Sales Return
                    this.ubtfaxNo.Visible = true;
                    strSysName = "OE";
                    strSql = "select  CustNo,ReturnNo,OrdStatus from oeReturnHdr " +
                             " where returnno in ( select min(returnNo) from oeReturnHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : " ") + " group by CustNo ) order by ReturnNo";
                    dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    strSql = "select  recid,HeadOffice,InvoiceTo,CustNo,Fax,Email,Contact,BillToFax  from arCustomer where custno in (select   CustNo from oeReturnHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : " ") + " )";
                    dvInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    strSql = "select  a.recid,a.CustNo,a.Fax,a.Emailaddress,a.Contact,b.ReturnNo,a.billtofax,a.BillEmail,a.BillToContact  from arshipto a inner join (select   CustNo,ShipToNo,ReturnNo from oeReturnHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " ) b on a.custno=b.custno and a.shiptono=b.shiptono ";
                    dvShipToInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    blnMustUseBillToForInvWhenFaxAndEmail  = Customize.MustUseBillToForInvWhenFaxAndEmail(); 
                    foreach (DataRowView drModule in dvModule)
                    {
                        bool blnIsInvoice = false;
                        blnIsInvoice = CvtFunc.ToBoolean(CvtFunc.ToInt(drModule["OrdStatus"]) >= 50 ? true : false);
                        if (!blnMustUseBillToForInvWhenFaxAndEmail || !blnIsInvoice )
                        {
                            dvInfo.RowFilter = "CustNo='" + drModule["CustNo"].ToString() + "'";
                            mstrCustSupNo = drModule["CustNo"].ToString();
                            if (dvInfo.Count > 0)
                            {
                                switch (intJobType)
                                {
                                    case FaxJobType.intFax:
                                        if (SysVar.CodeSignNo == SysVar.DefineCompany.Pacific && dvInfo[0]["BillToFax"].ToString().Trim() != string.Empty)
                                            strSendTo += dvInfo[0]["BillToFax"].ToString().Trim() + ";";
                                        else
                                            strSendTo += dvInfo[0]["Fax"].ToString().Trim() + ";";
                                        break;
                                    case FaxJobType.intEmail:
                                        strSendTo += dvInfo[0]["Email"].ToString().Trim() + ";";
                                        break;
                                }
                                strSendToName += dvInfo[0]["Contact"].ToString().Trim() + ";";
                            }
                        }
                        else
                        {
                            
                                dvInfo.RowFilter = "CustNo='" + drModule["CustNo"].ToString() + "'";
                                dvShipToInfo.RowFilter = "ReturnNo='" + drModule["ReturnNo"].ToString() + "'";
                                mstrCustSupNo = drModule["CustNo"].ToString();
                                if (dvShipToInfo.Count > 0)
                                {
                                    switch (intJobType)
                                    {
                                        case FaxJobType.intFax:
                                            if (dvShipToInfo[0]["billtofax"].ToString().TrimEnd().Length > 0)
                                                strSendTo += dvShipToInfo[0]["billtofax"].ToString().Trim() + ";";
                                            else
                                                strSendTo += dvInfo[0]["Fax"].ToString().Trim() + ";";
                                            break;
                                        case FaxJobType.intEmail:
                                            if (dvShipToInfo[0]["BillEmail"].ToString().TrimEnd().Length > 0)
                                                strSendTo += dvShipToInfo[0]["BillEmail"].ToString().Trim() + ";";
                                            else
                                                strSendTo += dvInfo[0]["Email"].ToString().Trim() + ";";
                                            break;
                                    }
                                    if (dvShipToInfo[0]["BillToContact"].ToString().TrimEnd().Length > 0)
                                        strSendToName += dvShipToInfo[0]["BillToContact"].ToString().Trim() + ";";
                                    else
                                        strSendToName += dvInfo[0]["Contact"].ToString().Trim() + ";";
                                }
                                else
                                {
                                    switch (intJobType)
                                    {
                                        case FaxJobType.intFax:                                 
                                            strSendTo += dvInfo[0]["Fax"].ToString().Trim() + ";";
                                            break;
                                        case FaxJobType.intEmail:                                            
                                            strSendTo += dvInfo[0]["Email"].ToString().Trim() + ";";
                                            break;
                                    }                                    
                                    strSendToName += dvInfo[0]["Contact"].ToString().Trim() + ";";
                                }                            
                        }
                        //strSql = "INSERT INTO #tmpLog ( Sys, Invno ,CustNo) " +
                        //         " VALUES ('" + strSysName + "','" + drModule["ReturnNo"].ToString() + "','" + drModule["CustNo"].ToString().Trim() + "')";
                        //mdbcon.ExecuteSql(strSql);
                    }
                    break;
                    #endregion
            case FaxEmailType.PurchaseOrder:
                    #region P/O
                    this.ubtfaxNo.Visible = true;
                    strSysName = "OE";
                    bool blnPOFillData=false;
                    mstrLocNo = DataFunctions.GetFieldValue(mdbcon, "PoOrderHdr", "LocNo", strCondition);

                    if (intJobType == FaxJobType.intFax)
                    {
                        strSql = "select  distinct a.SupNo,a.Fax,a.Contact from apordfrom a inner join  (select SupNo,OrderFromNo from PoOrderHdr  where OrdNo in ( select min(OrdNo) from PoOrderHdr " + (strCondition.Length > 0 ? " where " + strCondition : " ") + " )) b on a.supno=b.supno and a.ordfromno=b.OrderFromNo where a.Fax<>'' ";
                        dvInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                        foreach (DataRowView drModule in dvInfo)
                        {
                            blnPOFillData = true;

                            mstrCustSupNo = drModule["SupNo"].ToString();
                            switch (intJobType)
                            {
                                case FaxJobType.intFax:
                                    strSendTo += drModule["Fax"].ToString().Trim() + ";";
                                    break;                                
                            }
                            strSendToName += drModule["Contact"].ToString().Trim() + ";";
                        }
                        
                    }
                    if (!blnPOFillData)
                    {
                        strSql = "select SupNo,Fax,Email,Contact from apSupplier where SupNo in (select SupNo from PoOrderHdr  where OrdNo in ( select min(OrdNo) from PoOrderHdr " + (strCondition.Length > 0 ? " where " + strCondition : " ") + " group by SupNo ) )";
                        dvInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                        foreach (DataRowView drModule in dvInfo)
                        {
                            mstrCustSupNo = drModule["SupNo"].ToString();
                            switch (intJobType)
                            {
                                case FaxJobType.intFax:
                                    strSendTo += drModule["Fax"].ToString().Trim() + ";";
                                    break;
                                case FaxJobType.intEmail:
                                    strSendTo += drModule["Email"].ToString().Trim() + ";";
                                    break;
                            }
                            strSendToName += drModule["Contact"].ToString().Trim() + ";";
                        }
                    }

                    //strSql = "select SupNo from PoOrderHdr "
                    //strSql = strSql & vbCrLf & " where OrdNo in ( select min(OrdNo) from PoOrderHdr " & IIf(Len(strCondition) > 0, " where " & strCondition, " ") & " group by SupNo ) order by OrdNo"
                    //Set rsModule = gcn.Execute(strSql)
                    //strSql = "select SupNo,Fax,Email,Contact from apSupplier where SupNo in (select  SupNo from PoOrderHdr " & IIf(Len(strCondition) > 0, " where " & strCondition, " ") & " )"
                    //Set rsInfo = gcn.Execute(strSql)
                    
                    //While Not rsModule.EOF
                    //    rsInfo.Filter = "SupNo='" & rsModule!SUPNo & "'"
                    //    mstrCustSupNo = rsModule!SUPNo
                    //    If Not rsInfo.EOF Then
                    //        Select Case intJobType
                    //            Case FaxJobType.gIntFax
                    //                strSendTo = strSendTo & RTrim(rsInfo!Fax) & ";"
                    //            Case FaxJobType.gIntEmail
                    //                strSendTo = strSendTo & RTrim(rsInfo!Email) & ";"
                    //        End Select
                    //        strSendToName = strSendToName & RTrim(rsInfo!Contact) & ";"
                    //    End If
                    //    rsModule.MoveNext
                    //Wend
                    
                    //If Trim(strFaxMail) <> "" Then
                    //    strSendTo = strFaxMail
                    //    strSendToName = strContact
                        
                    //    txtFields(0).Text = strSendTo
                    //    txtFields(1).Text = strSendToName
                    //    txtFields(4).Text = strSender
                    //End If

                    break;
                    #endregion
                case FaxEmailType.BillToForInv:
                    #region Customer
                    this.ubtfaxNo.Visible = true;
                    blnMustUseBillToForInvWhenFaxAndEmail = Customize.MustUseBillToForInvWhenFaxAndEmail();
                    if (!blnMustUseBillToForInvWhenFaxAndEmail)
                        strSql = "select  recid,CustNo,Fax,Email,Contact  from arCustomer  " + (strCondition.Length > 0 ? " where " + strCondition : " ") +
                        " order by CustNo";
                    else
                    {
                        strSql = "select  recid,A.CustNo,Fax,Email,Contact from arCustomer A inner join (select   CustNo,ShipToNo,InvNo from oeOrderHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " ) B on a.custno=b.custno";
                        dvCust = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;   
                        strSql = "select  a.recid,a.CustNo,a.Fax,a.Emailaddress,a.Contact,b.InvNo,a.billtofax,a.BillEmail,a.BillToContact  from arshipto a inner join (select   CustNo,ShipToNo,InvNo from oeOrderHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " ) b on a.custno=b.custno and a.shiptono=b.shiptono ";
                    }
                    dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;

   
                    
                        
                        if (!blnMustUseBillToForInvWhenFaxAndEmail)
                        {
                            foreach(DataRowView drModule in dvModule)
                            {
                                mstrCustSupNo = drModule["CustNo"].ToString();
                                switch (intJobType)
                                {
                                    case FaxJobType.intFax:
                                        strSendTo += drModule["Fax"].ToString().Trim() + ";";
                                        break;
                                    case FaxJobType.intEmail:
                                        strSendTo += drModule["Email"].ToString().Trim() + ";";
                                        break;
                                }
                                strSendToName += drModule["Contact"].ToString().Trim() + ";";
                            }
                        }
                        else
                        {
                            if (dvModule.Count > 0)
                            {
                                foreach (DataRowView drModule in dvModule)
                                {
                                    switch (intJobType)
                                    {                                        
                                        case FaxJobType.intFax:
                                            if ( drModule["billtofax"].ToString().TrimEnd().Length >0)
                                                strSendTo += drModule["billtofax"].ToString().Trim() + ";";
                                            else
                                                strSendTo += dvCust[0]["fax"].ToString().Trim() + ";";
                                            break;
                                        case FaxJobType.intEmail:
                                            if ( drModule["BillEmail"].ToString().TrimEnd().Length >0  )
                                                strSendTo += drModule["BillEmail"].ToString().Trim() + ";";
                                            else
                                                strSendTo += dvCust[0]["Email"].ToString().Trim() + ";";
                                            break;
                                    }
                                    strSendToName += drModule["BillToContact"].ToString().Trim() + ";";
                                }
                            }
                            else
                            {
                                foreach (DataRowView drCust in dvCust)
                                {
                                    mstrCustSupNo = drCust["CustNo"].ToString();
                                    switch (intJobType)
                                    {
                                        case FaxJobType.intFax:
                                            strSendTo += drCust["Fax"].ToString().Trim() + ";";
                                            break;
                                        case FaxJobType.intEmail:
                                            strSendTo += drCust["Email"].ToString().Trim() + ";";
                                            break;
                                    }
                                    strSendToName += drCust["Contact"].ToString().Trim() + ";";
                                }
                            }
                        }
                    
                    break;
                    #endregion 
                case FaxEmailType.Supplier:
                    #region Supplier
                    strSql = "select recid,Fax, Email,Contact,SupNo from apsupplier " + (strCondition.Length > 0 ? " where " + strCondition : " ") +
                        " order by SupNo";
                    dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    foreach (DataRowView drModule in dvModule)
                    {
                        mstrCustSupNo = drModule["SupNo"].ToString();
                        switch (intJobType)
                        {
                            case FaxJobType.intFax:
                                strSendTo += drModule["Fax"].ToString().Trim() + ";";
                                break;
                            case FaxJobType.intEmail:
                                strSendTo += drModule["Email"].ToString().Trim() + ";";
                                break;
                        }
                        strSendToName += drModule["Contact"].ToString().Trim() + ";";
                    }
                    break;
                    #endregion
                case FaxEmailType.ComputerChqEditList:
                    #region ComputerChqEditList
                    strSql = "select recid,Fax, Email,Contact,SupNo from apsupplier " + (strCondition.Length > 0 ? " where " + strCondition : " ") +
                        " order by SupNo";
                    dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    foreach (DataRowView drModule in dvModule)
                    {
                        mstrCustSupNo = drModule["SupNo"].ToString();
                        switch (intJobType)
                        {
                            case FaxJobType.intFax:
                                strSendTo += drModule["Fax"].ToString().Trim() + ";";
                                break;
                            case FaxJobType.intEmail:
                                strSendTo += drModule["Email"].ToString().Trim() + ";";
                                break;
                        }
                        strSendToName += drModule["Contact"].ToString().Trim();
                    }
                    break;
                    #endregion
                case FaxEmailType.Transportation:
                    #region Transpatioin
                    strSql = "select * from TpTranspHdr " + (strCondition.Length > 0 ? " where " + strCondition : " ");
                    DataRow drTrans = mdbcon.GetDataFirstRow(strSql);
                    string strType = string.Empty;
                    string strDocNo= string.Empty ;
                    string strSourceDBconnectString = string.Empty;
                    if (drTrans != null)
                    {
                        strType = CvtFunc.ToString ( drTrans ["Type"]);
                        strDocNo = CvtFunc.ToString ( drTrans["DocNo"]);
                        strSourceDBconnectString = CvtFunc.ToString(drTrans["SourceDBconnectString"]);
                        if (strSourceDBconnectString != "")
                        {
                            strDocNo = DataFunctions.GetFieldValue ( mdbcon, "TP_OeOrderHdr","DesticonInvNo","invno='"+ strDocNo +"'");
                        }
                        if  ( strType =="O" )  // Sales Order
                        {
                            
                            switch (intJobType)
                            {
                                 case FaxJobType.intFax:
                                     strSql ="select SupNo,Fax,Contact from apOrdFrom where SupNo in (select top 1 SupNo from oeTruckCharge   where ordno='" + strDocNo +"' order by recid)";
                                     break ;
                                 case FaxJobType.intEmail :
                                     strSql ="select SupNo,Fax,Email,Contact from apSupplier where SupNo in (select top 1 SupNo from oeTruckCharge   where ordno='" + strDocNo + "' order by recid)";
                                     break ;
                             }
                        }                        
                        else if (strType == "U") //PO
                        {
                            switch (intJobType)
                            {
                                case FaxJobType.intFax:
                                    strSql = "select SupNo,Fax,Contact from apOrdFrom where SupNo in (select top 1 SupNo from tpTranspTruckCharge   where orderno='" + strDocNo +"' order by recid)";
                                    break;
                                case FaxJobType.intEmail:
                                    strSql = "select SupNo,Email,Contact from apSupplier where SupNo in (select top 1 SupNo from tpTranspTruckCharge   where orderno='" + strDocNo + "' order by recid)";
                                    break;
                            }
                        }
                        else if (strType == "R")  //Return
                        {
                            switch (intJobType)
                            {
                                case FaxJobType.intFax:
                                    strSql = "select SupNo,Fax,Contact from apOrdFrom where SupNo in (select top 1 SupNo from oeReturnTruckCharge   where Returnno='" + strDocNo + "' order by recid)";
                                    break;
                                case FaxJobType.intEmail:
                                    strSql = "select SupNo,Fax,Email,Contact from apSupplier where SupNo in (select top 1 SupNo from oeReturnTruckCharge   where Returnno='" + strDocNo + "' order by recid)";
                                    break;
                            }
                        }
                        else if (strType == "B")
                        {                         
                            return true;
                        }
                        else if (strType == "I")
                        {
                            return true;
                        }
                        else if (strType == "1")
                        {
                            return true;
                        }
                    }
                    dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    foreach (DataRowView drModule in dvModule)
                    {
                        mstrCustSupNo = CvtFunc.ToString(drModule["SupNo"]);
                        switch (intJobType)
                        {
                            case FaxJobType.intFax:
                                strSendTo += drModule["Fax"].ToString().Trim() + ";";
                                break;
                            case FaxJobType.intEmail:
                                strSendTo += drModule["Email"].ToString().Trim() + ";";
                                break;
                        }
                    }
                    #endregion
                    break; 
                case FaxEmailType.PullCard :
                    #region  PullCard
                    this.ubtfaxNo.Visible = true;
                    strSysName = "OE";
                    
                    strSql = "select  CustNo, invno from oeOrderHdr " +
                             " where invno in ( select min(invNo) from oeOrderHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " group by CustNo ) order by InvNo";
                    dvModule = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    strSql = "select  recid,CustNo,Fax,Email,Contact,BillToFax  from arCustomer where custno in (select   CustNo from oeOrderHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " )";
                    dvInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    strSql = "select  a.recid,fax,email,Contact  from mlocation a inner join (select LocNo from oeOrderHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " ) b on a.locationno=b.locno ";
                    dvShipToInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    
                    foreach (DataRowView drModule in dvModule)
                    {                       
                        mstrCustSupNo = drModule["CustNo"].ToString();
                        bool blnShipToInfo = false;
                        bool blnShipToContactInfo = false;

                       switch (intJobType)
                        {
                            case FaxJobType.intFax:
                                if (dvShipToInfo[0]["Fax"].ToString().Trim().Length > 0)
                                {
                                    strSendTo += dvShipToInfo[0]["Fax"].ToString().Trim() + ";";

                                    blnShipToInfo = true;
                                }
                                break;
                            case FaxJobType.intEmail:
                                if (dvShipToInfo[0]["Email"].ToString().Trim().Length > 0)
                                {
                                    strSendTo += dvShipToInfo[0]["Email"].ToString().Trim() + ";";

                                    blnShipToInfo = true;
                                }
                                break;
                        }
                        if (dvShipToInfo[0]["Contact"].ToString().Trim().Length > 0)
                        {
                            strSendToName += dvShipToInfo[0]["Contact"].ToString().Trim() + ";";
                            blnShipToContactInfo = true;
                        }
                        strSql = "INSERT INTO #tmpLog ( Sys, Invno ,CustNo) " +
                                 " VALUES ('" + strSysName + "','" + drModule["InvNo"].ToString() + "','" + drModule["CustNo"].ToString().Trim() + "')";
                        mdbcon.ExecuteSql(strSql);
                    }
                    //if (intJobType == FaxJobType.intEmail)
                    //{
                    //    strTmpSendTo = strSendTo;
                    //    strSql = "select recid,CustNo,ShipToNo,Fax,Contact,Emailaddress from arShipTo where custno+shiptono in (select   CustNo+shiptono from oeorderHdr " + (strCondition.Trim() != string.Empty ? " where " + strCondition : "") + " ) order by custno,shiptono";
                    //    DataView dvTable = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                    //    if (dvTable.Count > 0)
                    //        strSendTo = dvTable[0]["Emailaddress"].ToString().Trim() + ";";
                    //    if (strSendTo.Trim() == ";")
                    //        strSendTo = strTmpSendTo;
                    //}
                    #endregion
                    break;
                case FaxEmailType.EnternalRM:
                    #region RM
                    this.ubtfaxNo.Visible = true;
                    strSysName = "RM";
                    {
                        strSql = "select SupNo,Fax,Email,Contact from apSupplier where SupNo in (select SupNo from rmTruckCharge  where OrdNo in ( select OrdNo from rmWkOrderHdr " + (strCondition.Length > 0 ? " where " + strCondition : " ") + " ) and Type='R' )";
                        dvInfo = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
                        foreach (DataRowView drModule in dvInfo)
                        {
                            mstrCustSupNo = drModule["SupNo"].ToString();
                            switch (intJobType)
                            {
                                case FaxJobType.intFax:
                                    strSendTo += drModule["Fax"].ToString().Trim() + ";";
                                    break;
                                case FaxJobType.intEmail:
                                    strSendTo += drModule["Email"].ToString().Trim() + ";";
                                    break;
                            }
                            strSendToName += drModule["Contact"].ToString().Trim() + ";";
                        }
                    }
                    #endregion
                    break;
            }

            #region
 
            if (moduleType ==  FaxEmailType.Transportation)
            {
                //strSendTo = " ";
                if (intType == 13)
                {
                    switch(intJobType)
                    {
                        case FaxJobType.intFax:
                            if (strSendTo.Substring(strSendTo.Length-1,1) == ";")
                            {
                                txtFaxNo.Text = strSendTo.Substring(0,strSendTo.Length - 1);
                                if (strSendToName.Length > 0)
                                    txtTo.Text = strSendToName.Substring(0, strSendToName.Length - 1);
                                else
                                    txtTo.Text = strSendToName;
                            }
                            break;
                        case FaxJobType.intEmail:
                            if (DataFunctions.GetmMiscValue("CustomsFormEmail", "TP") != string.Empty && 
                                DataFunctions.GetmMiscValue("CustomsFormTo", "TP") != string.Empty)
                            {
                                txtFaxNo.Text = DataFunctions.GetmMiscValue("CustomsFormEmail", "TP");
                                if (strSendToName.Length > 0)
                                    txtTo.Text = strSendToName.Substring(0, strSendToName.Length - 1);
                                else
                                    txtTo.Text = strSendToName;
                            }
                            else
                            {
                                txtFaxNo.Text = strSendTo.Substring(0, strSendTo.Length - 1);
                                if (strSendToName.Length > 0)
                                    txtTo.Text = strSendToName.Substring(0, strSendToName.Length - 1);
                                else
                                    txtTo.Text = strSendToName;
                            }
                            break;
                    }
                }
                else
                {
                    if (strSendTo.Length > 0 && strSendTo.Substring(strSendTo.Length - 1, 1) == ";")
                    {
                        txtFaxNo.Text = strSendTo.Substring(0, strSendTo.Length - 1);
                        if (strSendToName.Length > 0)
                            txtTo.Text = strSendToName.Substring(0, strSendToName.Length - 1);
                        else
                            txtTo.Text = strSendToName;
                    }
                }
            }
            else
            {
                if (strSendTo.Length >0 && strSendTo.Substring(strSendTo.Length-1,1) == ";")
                {
                    txtFaxNo.Text = strSendTo.Substring(0,strSendTo.Length - 1);
                    if (strSendToName.Length > 0)
                        txtTo.Text = strSendToName.Substring(0, strSendToName.Length - 1);
                    else
                        txtTo.Text = strSendToName;
                }
            }
            #endregion

            #region 
            txtSubject.Text = strSubject;
            
            switch(intJobType)
            {
                case FaxJobType.intNoJob:
                    return false;
                case FaxJobType.intFax:
                    txtSender.Text = DataFunctions.GetmMiscValue("CompanyFax", "m");
                    if (Customize.UseLookUpInFaxAndEmail())
                        ubtfaxNo.Visible = true;
                    else
                        ubtSender.Visible = false;

                    lblFaxNo.Text = "Fax Number :";
                    lblSender.Text = "Sender Fax :";

                    lblExample.Text = "Fax Number : 1122;3344";
                    btnOk.Text = "&Fax";
                    this.Text = "Enter Fax Information";
                    break;
                case FaxJobType.intEmail:
                    txtSender.Text = DataFunctions.GetFieldValue("mUser", "EmailAddress", "username='" + SysVar.UserName + "'");
                    ubtSender.Visible = true;
                    if ( Customize.UseLookUpInFaxAndEmail() )
                    {
                        ubtfaxNo.Visible = true;
                    }
                    lblFaxNo.Text = "E-Mail :";
                    lblSender.Text = "Sender E-Mail :";

                    lblExample.Text = "E-Mail : 1@1.com;2@2.com";
                    btnOk.Text = "&Send";
                    this.Text = "Enter E-Mail Information";
                    break;
                default:
                    return false;

            }

            if (blnDefault)
            {
                if (txtFaxNo.Text.Trim() == string.Empty)
                {
                    this.ShowDialog();
                }
                else
                {
                    btnOk_Click(null, null);
                }
            }
            else
            {
                this.ShowDialog();
            }
            strTitle = mstrFaxTitle;
            strToName = mstrToName;
            return mblnOK;
            #endregion
        }
        public bool GetFaxTitle(ref string strTitle, FaxJobType intJobType, FaxEmailType moduleType, string strCondition, string strSubject, int intType, bool blnIsForMultBOl, string strToName)
        {
            return GetFaxTitle(ref  strTitle,  intJobType,  moduleType,  strCondition,  strSubject,  intType,  blnIsForMultBOl,  strToName, false);
        }
        public bool GetFaxTitle(ref string strTitle, FaxJobType intJobType, FaxEmailType moduleType, string strCondition, string strSubject, int intType,   string strToName)
        {
            return GetFaxTitle(ref strTitle, intJobType, moduleType, strCondition, strSubject, 1, false, strToName,false);
        }
        public bool GetFaxTitle(ref string strTitle, FaxJobType intJobType, string strSubject)
        {
            return GetFaxTitle(ref strTitle, intJobType, FaxEmailType.Other, "", strSubject, 1, false, "",false);
        }
        protected override void ubtLookUp_Click(object sender, EventArgs e)
        {
            string strSql = string.Empty;
            ButtonEx ubt = sender as ButtonEx;
            ToolStripMenuItem tsm = null;
            if (ubt == null) { tsm = sender as ToolStripMenuItem; }
            if (ubt == null && tsm == null) { return; }

            bool blnHasSetFocus = false;
            Control con = null;
            string strSenderName = ubt != null ? ubt.Name : tsm.Name;

            string strCaption = string.Empty;
            string strFindValue = string.Empty;
            string strLookUpField = string.Empty;

            int intFieldIndex;
            DBAccess dbcon = new DBAccess(false, false);
            switch (strSenderName)
            {
                case "ubtfaxNo":
                case "bySupplierToolStripMenuItem":
                case "byLocationToolStripMenuItem":
                    intFieldIndex = 0;
                    switch((FaxJobType)mIntJobType)
                    {
                        case FaxJobType.intFax:
                            strCaption = "Fax";
                            if (Customize.UseLookUpInFaxAndEmail())
                                strLookUpField = "FaxNo";
                            else
                                strLookUpField = "Fax";
                            break;
                        case FaxJobType.intEmail:
                            strCaption = "Email";
                            if (Customize.UseLookUpInFaxAndEmail () && mModule ==  FaxEmailType.Transportation1)
                                strLookUpField = "Email";
                            else if (Customize.UseLookUpInFaxAndEmail ())
                                strLookUpField = "Email";
                            else
                                strLookUpField = "EmailAddress";
                            break;
                    }
                    switch (mModule)
                    {
                        case FaxEmailType.SaledOrder :
                        case FaxEmailType.Customer :
                        case FaxEmailType.SalesReturn:
                            strSql = @"CREATE TABLE [#tmpArContact] (Recid  int NOT NULL IDENTITY (1, 1),
                                        [CustNo] [char] (8) NOT NULL default '',
                                        [Contact] [char] (30) NOT NULL default '',
                                        [Fax] [char] (15) NOT NULL default '',
                                        [EmailAddress] [char] (40) NOT NULL default '')";
                            dbcon.ExecuteSql(strSql);
                            strSql = @"insert into #tmpArContact (CustNo,Contact,Fax,EmailAddress) "+
                                      "(select CustNo,Contact,Fax,EmailAddress from arContact " + (mstrCustSupNo.Trim() != string.Empty ? " where CustNo='" + mstrCustSupNo + "'" : "") +
                                     ")union (select CustNo,Contact,Fax,Emailaddress from ArShipTo " + (mstrCustSupNo.Trim() != string.Empty ? " where CustNo='" + mstrCustSupNo + "'" : "")+ ")";
                            dbcon.ExecuteSql(strSql);
                            strSql = @"select CustNo,Contact,Fax,EmailAddress from #tmpArContact order by Custno";
                            break;
                        case FaxEmailType.PurchaseOrder :
                            if ((mIntJobType == (int)FaxJobType.intFax || mIntJobType == (int)FaxJobType.intEmail) && Customize.UseByLocationInFaxLookUp() && strSenderName == "ubtfaxNo")
                            {
                                this.mnuFaxLookup.Show(this.ActiveControl, new Point(0, 0));
                                return;
                            }

                            if (strSenderName == "ubtfaxNo" || strSenderName == "bySupplierToolStripMenuItem")
                            {
                                strSql = "select SupNo,Contact,Fax,EmailAddress from apContact " + (mstrCustSupNo.Trim() != string.Empty ? " where SupNo='" + mstrCustSupNo + "'" : "") + " order by SupNo ,Contact";
                                switch ((FaxJobType)mIntJobType)
                                {
                                    case FaxJobType.intFax:

                                        strLookUpField = "Fax";
                                        break;
                                    case FaxJobType.intEmail:

                                        strLookUpField = "EmailAddress";
                                        break;
                                }
                            }
                            else
                            {
                                strSql = "select LocationNo,Fax,Email,Contact from mLocation order by LocationNo" + (mstrLocNo.Trim() != string.Empty ? " where LocationNo='" + mstrLocNo + "'" : "");
                                switch ((FaxJobType)mIntJobType)
                                {
                                    case FaxJobType.intFax:

                                        strLookUpField = "Fax";
                                        break;
                                    case FaxJobType.intEmail:

                                        strLookUpField = "Email";
                                        break;
                                }
                            }
                            break;
                        case FaxEmailType.InventoryTransfer : 
                            strSql = "select LocationNo,Fax,Email,Contact from mLocation order by LocationNo";
                            break;
                        case FaxEmailType.Transportation :
                            strSql = "select truckername,faxno as fax,email as EmailAddress,'' as Contact from TpTruckFaxEmail order by truckername";
                            break;
                        case FaxEmailType.Transportation1 :
                            strSql = "select supNo,Fax,Email,Contact  from apsuppLier " + (mstrCustSupNo.Trim() != string.Empty ? " where SupNo='" + mstrCustSupNo + "'" : "") + " order by SupNo ,Contact";
                            break;
                        default:
                            strSql = "select CustNo,Contact,Fax,EmailAddress from arContact " + (mstrCustSupNo.Trim() != string.Empty ? " where CustNo='" + mstrCustSupNo + "'" : "") + " order by CustNo ,Contact";
                            break;
                    }
                    strFindValue = txtFaxNo.Text;
                    con = txtFaxNo;
                    break;
                case "ubtSender":
                    intFieldIndex = 4;
                    strSql = "select username,EmailAddress from muser order by username ";
                    strCaption = "Email";
                    strFindValue = txtSender.Text;
                    strLookUpField = "EmailAddress";
                    con = txtSender;
                    break;
                default:
                    break;
            }

            if (mblnLookUpSortByDescription) strSql = DataFunctions.GetSqlByDescriptionSort(strSql);
            mblnLookUpSortByDescription = false;
            WP.Modules.frmLookUp frmlookup = new WP.Modules.frmLookUp(dbcon,strSql, strCaption, strFindValue, strLookUpField);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                if (con != null)
                {
                    if (strSenderName == "ubtSender")
                        con.Text = frmlookup.GetReturnValue(strLookUpField);
                    else
                    {
                        con.Text += (con.Text.Trim() != string.Empty && con.Text.Substring(con.Text.Length - 1) != ";" ? ";" : "") + frmlookup.GetReturnValue(strLookUpField) + ";";
                        if(txtTo.Text.Trim()!="") txtTo.Text +=  ";";
                    }
                    if (!blnHasSetFocus)
                    {
                        con.Focus();
                        blnHasSetFocus = true;
                    }
                }
            }
            dbcon.CloseCon();
            frmlookup.Close();
        }

        private void frmFax_FormClosing(object sender, FormClosingEventArgs e)
        {
            mdbcon.CloseCon();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string[] strSendTo;
            string[] strSendToName;
            int intSendToCount, intSendToNameCount;

            string strRemark = string.Empty;
            string strSql = string.Empty;
            mstrFaxTitle = string.Empty;

            #region 
            if (txtFaxNo.Text.Trim() == string.Empty)
            {
                switch((FaxJobType)mIntJobType)
                {
                    case FaxJobType.intFax:
                        MsgBoxEx.Show(Msg.Select("You must enter fax number for customer " + mstrCustSupNo + "!", DataFunctions.GetMsg(this.Name, "MSG_MustEnterFaxNumber", new string[] { mstrCustSupNo })), SysVar.WarnTitle);
                        mblnOK = false;
                        return;
                        //break;
                    case FaxJobType.intEmail:
                        MsgBoxEx.Show(Msg.Select("You must enter E-Mail address for customer " + mstrCustSupNo + "!", DataFunctions.GetMsg(this.Name, "MSG_MustEnterEMailAddress", new string[] { mstrCustSupNo })), SysVar.WarnTitle);
                        mblnOK = false;
                        return;
                        //break;
                }
                return;
            }
            txtFaxNo.Text = txtFaxNo.Text.TrimEnd();
            if (txtFaxNo.Text.Length != 0 && txtFaxNo.Text.Substring(txtFaxNo.Text.Length - 1, 1) == ";")
                txtFaxNo.Text = txtFaxNo.Text.Substring(0, txtFaxNo.Text.Length - 1);

            if (txtTo.Text.Length != 0 && txtTo.Text.Substring(txtTo.Text.Length - 1, 1) == ";")
                txtTo.Text = txtTo.Text.Substring(0, txtTo.Text.Length - 1);

            strSendTo = txtFaxNo.Text.TrimEnd().Split(';');
            strSendToName = txtTo.Text.TrimEnd().Split(';');
            intSendToCount = strSendTo.Length;
            intSendToNameCount = strSendToName.Length;
            #endregion

            #region switch
            switch ((FaxJobType)mIntJobType)
            {
                case FaxJobType.intFax:
                    for (int i=0; i<intSendToCount; i++)
                    {
                        //to
                        if (i>=intSendToNameCount)
                            ;
                        else
                        {
                            mstrFaxTitle += "@F201 " + strSendToName[i].TrimEnd() + "@";
                            strRemark += "(Send to )Name:" + strSendToName[i].TrimEnd() + "  ";
                        }
                        //fax number
                        mstrFaxTitle += "@F211 " + strSendTo[i].TrimEnd() + "@";
                        strRemark += "Fax:" + strSendTo[i].TrimEnd() + "   ";
                        if (i != intSendToCount)
                            mstrFaxTitle += "@F299@";
                    }
                    //Sender fax number
                    if (txtSender.Text.TrimEnd() != string.Empty)
                    {
                        mstrFaxTitle += "@F110 " + txtSender.Text.TrimEnd() + "@";
                        strRemark += "(From )Fax:" + txtSender.Text.TrimEnd();
                    }
                    break;
                case FaxJobType.intEmail:
                    for (int i=0; i<intSendToCount; i++)
                    {
                        //to
                        if (i>=intSendToNameCount)
                            ;
                        else
                        {
                            mstrFaxTitle += "@F201 " + strSendToName[i].TrimEnd() + "@";
                            strRemark += "(Send to )Name:" + strSendToName[i].TrimEnd() + "  ";
                        }
                        //E-Mail
                        mstrFaxTitle += "@F212 " + strSendTo[i].TrimEnd() + "@";
                        strRemark += "Mail:" + strSendTo[i].TrimEnd() + "   ";
                        if (i != intSendToCount)
                            mstrFaxTitle += "@F299@";
                    }
                    //sender E-Mail
                    if (txtSender.Text.TrimEnd() != string.Empty)
                    {
                        mstrFaxTitle += "@F111 " + txtSender.Text.TrimEnd() + "@";
                        strRemark += "(From )Mail:" + txtSender.Text.TrimEnd();
                    }
                    break;
            }
            #endregion
            //Sender name
            string strUsername = DataFunctions.GetmMiscValue("FaxUserName", "m").TrimEnd() ;
            if (strUsername != "")
            {
                mstrFaxTitle += "@F101 " + strUsername + "@";
            }
            //subject
            mstrFaxTitle += (txtSubject.Text.TrimEnd() != string.Empty ? "@F307 " + txtSubject.Text.TrimEnd() + "@" :string.Format ("@F307 {0}@",(char)(127)));
            //Conver page
            mstrFaxTitle += (txtCoverPage.Text.TrimEnd() != string.Empty ? "@F305 " + txtCoverPage.Text.TrimEnd() + "@" : "");
            if (txtComments.Text.TrimEnd().Length > 0)
            {
                mstrFaxTitle += "@F317 " + txtComments.Text.TrimEnd().Replace(Environment.NewLine,@"\n") + "@";
            }
    
            strSql = "select  Sys,InvNo, CustNo  from #tmpLog ";
            DataView dvLog = mdbcon.GetDataSet(strSql).Tables[0].DefaultView;
            strRemark = strRemark.Replace("'","''");
            foreach (DataRowView drLog in dvLog)
            {
                DataFunctions.AddLog(mdbcon, strRemark, drLog["Sys"].ToString(), drLog["InvNo"].ToString());
            }
            mblnOK = true;
            this.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            mblnOK = false;
            this.Close();
        }

        private void txtTo_TextChanged(object sender, EventArgs e)
        {
            mstrToName = txtTo.Text;
        }

        private void btnCov_Click(object sender, EventArgs e)
        {
    //            Dlg.InitDir = IIf(Right(App.Path, 1) = "\", App.Path & "CovPage", App.Path & "\CovPage")
    //Dlg.Filter = "Cover Page Files (*.cov)|*.cov|All Files (*.*)|*.*"
    //Dlg.FilterIndex = 0
    //Dlg.CancelError = True
    //Dlg.DialogTitle = "Open Cover Page"
    //Dlg.Flags = cdlOFNFileMustExist Or cdlOFNPathMustExist
    //Dlg.ShowOpen
    
    //txtFields(3).text = Dlg.FileName

            openFileDialog1.InitialDirectory = "";
            openFileDialog1.DefaultExt = "cov";
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "Cover Page Files (*.cov)|*.cov|All Files (*.*)|*.*";
            openFileDialog1.ShowDialog();
            if (openFileDialog1.FileName.Trim() != "")
            {
                txtCoverPage.Text = openFileDialog1.FileName;

            }
        }

        #region Private Events
        #endregion

        #region Private Functions
        #endregion

        
    }
}